//
//  SheetViewController.m
//  体育商城
//
//  Created by  on 2022/9/24.
//

#import "SheetViewController.h"
#import "SheetCell.h"

@interface SheetViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UIView *bgView;
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation SheetViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    for (NSLayoutConstraint *c in self.view.constraints) {
        if (c.firstAttribute == NSLayoutAttributeHeight && ([c.firstItem isEqual:self.tableView])) {
            c.constant = self.array.count * 50 + 60;
        }
    }
    [self.tableView layoutIfNeeded];
    [self.tableView reloadData];
}
- (void)initUI{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.scrollEnabled = NO;
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.tableView registerNib:[SheetCell nib] forCellReuseIdentifier:[SheetCell ID]];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.view addSubview:self.tableView];
    NSLayoutConstraint *height = [self.tableView a1:NSLayoutAttributeHeight e:NSLayoutRelationEqual t:nil a2:NSLayoutAttributeNotAnAttribute c:100];
    NSLayoutConstraint *left = [self.tableView a1:NSLayoutAttributeLeft e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeLeft c:0];
    NSLayoutConstraint *right = [self.tableView a1:NSLayoutAttributeRight e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeRight c:0];
    NSLayoutConstraint *bottom = [self.tableView a1:NSLayoutAttributeBottom e:NSLayoutRelationEqual t:self.bgView a2:NSLayoutAttributeBottom c:0];
    [self.view addConstraints:@[height,left,right,bottom]];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return section == 0 ? self.array.count : 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SheetCell *cell = [tableView dequeueReusableCellWithIdentifier:[SheetCell ID]];
    if (indexPath.section == 0 && self.array.count > 0){
        cell.line.hidden = (indexPath.row == self.array.count - 1);
        cell.title.text = LS(self.array[indexPath.row]);
        cell.title.textHexColor = @"#323232";
    }else{
        cell.line.hidden = YES;
        cell.title.text = LS(@"取消");
        cell.title.textHexColor = @"#AAADB3";
    }
    return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self dismissViewControllerAnimated:NO completion:nil];
    if (indexPath.section == 0) {
        if (self.block) {
            self.block((int)indexPath.row,self.array[indexPath.row]);
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return section == 0 ? 10 : 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return  nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    return view;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 && indexPath.row == 0){
        UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:cell.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(20, 20)];
        CAShapeLayer *layer = [CAShapeLayer layer];
        layer.frame = cell.bounds;
        layer.path = path.CGPath;
        cell.layer.mask = layer;
    }else{
        cell.layer.mask = nil;
    }
}
@end
